package ${domain}.frame.mail;

import ${domain}.frame.auth.LocalData;
import ${domain}.frame.mail.message.WFileMessage;
import ${domain}.frame.mail.message.WHtmlMessage;
import ${domain}.frame.mail.message.WInlineMessage;
import ${domain}.frame.mail.message.WMessage;
import ${domain}.frame.mail.message.WTextMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件发送者
 * <p>
 * 注注注: JavaMailSender发送器，可通过Springboot配置注入
 * 当发送多个邮箱时，需要多个发送器，可以构造多个
 * JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
 * javaMailSender.setDefaultEncoding(UTF-8);
 * javaMailSender.setHost(smtp.example.com);
 * javaMailSender.setUsername(**@example.com);
 * javaMailSender.setPassword(******);
 * javaMailSender.setPort(25);
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-08-01
 */
public class WMailSender {

    private static boolean send(WHtmlMessage message, JavaMailSender sender) {
        try {
            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(message.getFrom());
            helper.setTo(message.getTo());
            helper.setSubject(message.getSubject());
            helper.setText(message.getContent(), true);
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static boolean send(WTextMessage message, JavaMailSender sender) {
        try {
            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(message.getFrom());
            helper.setTo(message.getTo());
            helper.setSubject(message.getSubject());
            helper.setText(message.getContent());
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static boolean send(WFileMessage message, JavaMailSender sender) {
        try {
            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(message.getFrom());
            helper.setTo(message.getTo());
            helper.setSubject(message.getSubject());
            helper.setText(message.getContent(), true);
            if (message.getFiles() != null && message.getFiles().size() > 0) {
                for (File file : message.getFiles()) {
                    helper.addAttachment(file.getName(), file);
                }
            }
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static boolean send(WInlineMessage message, JavaMailSender sender) {
        try {
            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(message.getFrom());
            helper.setTo(message.getTo());
            helper.setSubject(message.getSubject());
            helper.setText(message.getContent(), true);
            if (message.getResMap() != null && message.getResMap().size() > 0) {
                for (String resId : message.getResMap().keySet()) {
                    FileSystemResource res = new FileSystemResource(message.getResMap().get(resId));
                    helper.addInline(resId, res);
                }
            }
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 发送邮件
     *
     * @param message 邮件对象
     * @return 是否发送成功
     */
    public static boolean send(WMessage message) {
        return send(message, null);
    }

    /**
     * 发送邮件
     *
     * @param message 邮件对象
     * @param sender  发送器
     * @return 是否发送成功
     */
    public static boolean send(WMessage message, JavaMailSender sender) {
        if (sender == null) {
            sender = LocalData.getBean(JavaMailSender.class);
        }
        if (sender == null) {
            throw new RuntimeException("not find JavaMailSender!");
        }
        if (message.getFrom() == null && sender instanceof JavaMailSenderImpl) {
            message.setFrom(((JavaMailSenderImpl) sender).getUsername());
        }
        if (message instanceof WFileMessage) {
            return send((WFileMessage) message, sender);
        } else if (message instanceof WInlineMessage) {
            return send((WInlineMessage) message, sender);
        } else if (message instanceof WHtmlMessage) {
            return send((WHtmlMessage) message, sender);
        } else if (message instanceof WTextMessage) {
            return send((WTextMessage) message, sender);
        } else {
            throw new RuntimeException("Unimplemented message type!");
        }
    }
}
